<!doctype html>
<html>
<head></head>
<body>
<script src="theme.js"></script>
<script>
  var isFirstRun = !localStorage.getItem('first_run_flag');
  var appliedThemeName = localStorage.getItem('applied_theme_name') ||
    'classic';
  var isThemeChanged = themeName != appliedThemeName;

  if (isFirstRun) {
    installTheme();
    localStorage.setItem('first_run_flag', true);
    localStorage.setItem('applied_theme_name', themeName);
  } else {
    if (isThemeChanged) {
      installTheme();
      localStorage.setItem('applied_theme_name', themeName);
    }
  }

  function installTheme() {
    if (themeUrl) {
      chrome.tabs.create({
        url: themeUrl
      });
    }
  }

  // Need to restart chrome to apply effect.
  function uninstallTheme() {
    chrome.management.getAll(function(extensions) {
      var length = extensions.length;
      for (var i = 0; i < length; i++) {
        var ext = extensions[i];
        if (ext.name == appliedThemeName) {
          chrome.management.uninstall(ext.id, function() {
            localStorage.setItem('last_theme_name', 'default');
          });
          return;
        }
      }
    });
  }
</script>
</body>
</html>